package com.xinqi.modules.sys.document.manager.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.sys.document.domain.DocumentManagerEntity;
import com.xinqi.modules.sys.document.dto.req.DocumentManagerQueryDTO;
import com.xinqi.modules.sys.document.dto.req.DocumentManagerTreeDTO;
import com.xinqi.modules.sys.document.dto.rsp.DocumentManagerResultDTO;
import com.xinqi.modules.sys.document.dto.rsp.DocumentManagerTreeResultDTO;
import com.xinqi.modules.sys.document.manager.DocumentManagerManager;
import com.xinqi.modules.sys.document.mapper.DocumentManagerMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 文档管理表管理层实现
 *
 * @author: gzy
 * @date: 2023/05/28
 */
@Service
@RequiredArgsConstructor
public class DocumentManagerManagerImpl extends BaseServiceImpl<DocumentManagerMapper, DocumentManagerEntity> implements DocumentManagerManager {

    /**
     * 自定义分页查询
     */
    @Override
    public Page<DocumentManagerResultDTO> findPage(Page<Object> page, DocumentManagerQueryDTO query) {
        return this.baseMapper.findPage(page, query);
    }

    /**
     * 自定义列表查询
     */
    @Override
    public List<DocumentManagerResultDTO> findList(DocumentManagerQueryDTO query) {
        return this.baseMapper.findList(query);
    }

    /**
     * 根据id查询 文档管理表数据
     */
    @Override
    public DocumentManagerEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return getById(id);
    }

    /**
     * 根据id删除 文档管理表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return this.removeById(id);
    }

    @Override
    public List<DocumentManagerResultDTO> findByParentId(Long parentId) {
        return this.baseMapper.selectAllParents(parentId);
    }

    @Override
    public List<DocumentManagerTreeResultDTO> findTree(DocumentManagerTreeDTO dto) {
        List<DocumentManagerTreeResultDTO> baseList = this.baseMapper.selectTreeBaseList(dto);
        return buildTreeUseStream(baseList, 0);
    }

    @Override
    public Page<DocumentManagerResultDTO> findByPage(Page<Object> page, DocumentManagerQueryDTO query) {
        return this.baseMapper.findByPage(page, query);
    }

    @Override
    public Boolean removeOutById(Long id) {
        return this.baseMapper.deleteOutById(id) > 0;
    }

    @Override
    public List<DocumentManagerResultDTO> findAll(DocumentManagerQueryDTO dto) {
        return this.baseMapper.selectAll(dto);
    }

    public static List<DocumentManagerTreeResultDTO> buildTreeUseStream(List<DocumentManagerTreeResultDTO> treeList, long id) {
        List<DocumentManagerTreeResultDTO> list = treeList.stream()
            //过滤父节点与传递的id相同的TreeNode对象
            .filter(treeNode -> treeNode.getParentId().longValue() == id)
            .map(treeNode -> {
                //递归设置孩子节点
                treeNode.setChildren(buildTreeUseStream(treeList, treeNode.getId()));
                return treeNode;
            }).collect(Collectors.toList());
        return list;
    }
}
